/**
 * 
 */
package com.mvc.service.healthmanager.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.huangqj.consant.StringUtils;
import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.dao.BaseDaoI;
import com.mvc.dao.mapper.BloodOxygenDao;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.healthmanager.archive.BloodOxygen;
import com.mvc.model.healthmanager.archive.BloodSugar;
import com.mvc.service.healthmanager.BloodOxygenServiceI;
import com.mvc.service.healthmanager.BloodSugarServiceI;
import com.mvc.utils.CreatHighChartJson;
import com.mvc.utils.StringUtil;

/**
 * @author qingjie
 *
 */
@Service
public class BloodOxygenServiceImpl implements BloodOxygenServiceI {
	private final String titleName = "血氧数据走势";
	private final String unit = "mmHg";
	private final String yAxixname = "血氧(mmHg)";
	@Autowired
	private BloodOxygenDao bOxygenDao;
	@Autowired
	private BaseDaoI<BloodOxygen> bDaoI;
	
	@Override
	public BloodOxygen getNewBloodOxygen(BloodOxygen bo) {
		Map<String, Object> params = StringUtils.beanToMap(bo);
		List<BloodOxygen> list = bDaoI.find(StringUtil.getHql("BloodOxygen", params, "desc"), params);
		return list.size()>0?list.get(0):new BloodOxygen();
	}

	@Override
	public List<BloodOxygen> getBloodOxygen(BloodOxygen bOxygen) {
		return bOxygenDao.getBloodOxygen(bOxygen);
	}

	@Override
	public String getJsonData(String personId) {
		List<BloodOxygen> list = bOxygenDao.getJsonData(personId);
		String[] xAxis = new String[list.size()];
		Map<String, Object> map = new HashMap<String, Object>();
		if(!list.isEmpty()){
			Map<String, Object> spo2_m = new HashMap<String, Object>();
			spo2_m.put("name", "血氧含量(mmHg)");
			List<Double> spo2_l = new ArrayList<Double>();
			Map<String, Object> pulse_m = new HashMap<String, Object>();
			pulse_m.put("name", "脉率(次/min)");
			List<Double> pulse_l = new ArrayList<Double>();
			
			for(int i=0; i<list.size(); i++){
				BloodOxygen bOxygen = list.get(i);
				xAxis[i] = GlobalConstant.SDF.format(bOxygen.getCreatDate());
				spo2_l.add(i, bOxygen.getSpo2());
				pulse_l.add(i, bOxygen.getPulse());
			}
			spo2_m.put("data", spo2_l);pulse_m.put("data", pulse_l);
			map.put("spo2", spo2_m);map.put("pulse", pulse_m);
		}
		return CreatHighChartJson.CreatJsonData(titleName, unit, yAxixname, xAxis, map);
	}

	@Override
	public List<BloodOxygen> findBloodOxygen(Map<String, Object> params,
			String order) {
		return bDaoI.find(StringUtil.getHql("BloodOxygen", params, order),params);
	}

	@Override
	public List<BloodOxygen> findBloodOxygens(Map<String, Object> params) {
		return bDaoI.find(StringUtil.getHql("BloodOxygen", params, "desc"),params);
	}

	@Override
	public BloodOxygen saveOrUpdate(BloodOxygen bo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("personId", bo.getPersonId());
		List<BloodOxygen> list = bDaoI.find(StringUtil.getHql("BloodOxygen", params, "desc"),params);
		if(list.size()>0){
			//设置服务次数
			int count = ValidatorUtil.isNullOnIntger(list.get(0).getCount())?0:list.get(0).getCount();
			bo.setCount(count+1);
			//设置身份证号码
			bo.setIdcode(list.get(0).getIdcode());
		}
		//设置时间
		//bo.setMeterDate(GlobalConstant.SDF2.format(bo.getCreatDate()));
		bo.setDataSourse("手机app");
		bDaoI.saveOrUpdate(bo);
		return bo;
	}
}
